package ee.videorental.serverside;

import java.util.LinkedList;

import ee.videorental.Message;

public class OutboundMessages 
{
	//FIFO
	private LinkedList<Message> outboundMessageQueue = new LinkedList<Message>();

	public synchronized void addMessage(Message messageToClient) 
	{
		outboundMessageQueue.add(messageToClient);
		System.out.println(messageToClient);
		this.notifyAll();
	}

	public synchronized Message getMessage() 
	{ 
		while (outboundMessageQueue.isEmpty()) 
		{
			try 
			{
				this.wait();
			}
			catch (InterruptedException e) 
			{
				System.out.println("Broadcaster has been interrupted, probably new message has been added!"); 
			}
		}

		Message messageToClient = outboundMessageQueue.getFirst();
		outboundMessageQueue.removeFirst();
		return messageToClient;
	}
}